/*
 * Created on 2004-04-20
 */
package org.opencube.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.StringTokenizer;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

/**
 * @author <a href="mailto:maciek@fingo.pl">FINGO - Maciej Mroczko</a>
 * TODO: comment
 */
public class Util
{
	/**
	 * Puts some white space before each line of the given string.
	 * 
	 * @param text - the string to be indented
	 * 
	 * @return String - the string with each line prefixed with some white space
	 */
	public static String indent( String text )
	{
		return indent( text, "  " );
	}
	/**
	 * Puts the spacer before each line of the given string.
	 * 
	 * @param text - the string to be indented
	 * @param spacer - the spacer to indent the string
	 * 
	 * @return String - the string with each line prefixed with the spacer
	 */
	public static String indent( String text, String spacer )
	{
		String result = Constants.STR_EMPTY;
		StringTokenizer st = new StringTokenizer( text, Constants.STR_NEW_LINE );
		while( st.hasMoreTokens() )
		{
			result += Constants.STR_NEW_LINE + spacer + st.nextToken();
		}
		
		return result;
	}
	/**
	 * Writes XML document into the given output stream.
	 *  
	 * @param document - the document to be written
	 * @param outputStream - the stream to write the document  
	 * 
	 * @throws TransformerConfigurationException
	 * @throws TransformerException
	 */
	public static void writeDocument( Document document, OutputStream outputStream )
	throws TransformerConfigurationException, TransformerException 
	{
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer serializer = transformerFactory.newTransformer();
		DOMSource dSource = new DOMSource( document );
		serializer.transform( dSource, new StreamResult( outputStream ) );	
	}
	/**
	 * Returns content of the given file as string.
	 * 
	 * @param file - the file to read
	 * 
	 * @return String - the content of the given file
	 * 
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static String getFileAsString( File file ) throws FileNotFoundException, IOException
	{
		return getInputStreamAsString( new FileInputStream( file ) );
	}
	/**
	 * Returns content of the given input string as string.
	 * 
	 * @param input string - the file to read
	 * 
	 * @return String - the content of the given input string
	 * 
	 * @throws IOException
	 */
	public static String getInputStreamAsString( InputStream is ) throws FileNotFoundException, IOException
	{
		StringBuffer sb = new StringBuffer();
		BufferedReader reader = new BufferedReader( new InputStreamReader( is ) );
		String line = null;
		while( ( line = reader.readLine() ) != null )
		{
			if( sb.length() > 0 )
			{
				sb.append( Constants.STR_NEW_LINE );
			}
			sb.append( line );
		}
		reader.close();
		
		return sb.toString();
	}
	/**
	 * Search for the given string in the array
	 * 
	 * @param array - the array to search in
	 * @param string - the string to find
	 * 
	 * @return boolean - <code>true</code>, if both parameters are not null and given value exists in the array
	 */
	public static boolean inArray( String[] array, String string )
	{
		for( int i = 0; array != null && string != null && i < array.length; i++ )
		{
			if( array[ i ].equals( string ) )
			{
				return true;
			}
		}
		return false;
	}
	/**
	 * Search for the given char in the array
	 * 
	 * @param array - the array to search in
	 * @param string - the char to find
	 * 
	 * @return boolean - <code>true</code>, if the given value exists in the array
	 */
	public static boolean inArray( char[] array, char c )
	{
		for( int i = 0; array != null && i < array.length; i++ )
		{
			if( array[ i ] == c )
			{
				return true;
			}
		}
		return false;
	}
}
